package com.school.service.impl;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.school.entity.HljtClassimage;
import com.school.entity.HljtNewsaddress;
import com.school.entity.HljtSchool;
import com.school.entity.HljtSchoolimage;
import com.school.entity.HljtSchoolnotify;
import com.school.entity.HljtTeacher;
import com.school.entity.HljtTeacherclass;

import com.school.entity.android.CampusGeneral;

import com.school.dao.HljtCampusDao;
import com.school.service.HljtCampusService;
import com.school.util.WhetherNotToUpdate;
@Service
public class HljtCampusServiceIpml implements HljtCampusService{
	
	@Autowired
	private HljtCampusDao hljtCampusDao;

	@Override
	@Transactional(readOnly=true)
	public HljtTeacher teacherSynopsis(HljtTeacherclass hljtTeacherclass) {
		
		return hljtCampusDao.teacherSynopsis(hljtTeacherclass);
	}

	@Override
	@Transactional(readOnly=true)
	public HljtSchool campusIntroduction(HljtSchoolimage hljtSchoolimage) {
		return hljtCampusDao.campusIntroduction(hljtSchoolimage);
	}

	@Override
	@Transactional(readOnly=true)
	public List<CampusGeneral> campusEnvironment(HljtSchoolimage hljtSchoolimage) {
		return hljtCampusDao.campusEnvironment(hljtSchoolimage);
	}

	@Override
	@Transactional(readOnly=true)
	public List<CampusGeneral> excellentSchoolClass(HljtClassimage hljtClassimage) {

		return hljtCampusDao.excellentSchoolClass(hljtClassimage);
	}

	@Override
	@Transactional(readOnly=true)
	public List<CampusGeneral> campusTeachersStrength() {
		return hljtCampusDao.campusTeachersStrength();
	}

	@Override
	@Transactional(readOnly=true)
	public List<HljtSchoolnotify> campusNotifications(HljtSchoolnotify hljt) {
		List<HljtSchoolnotify> temp = hljtCampusDao.campusNotifications(hljt);
		Map<Integer, HljtSchoolnotify> map = new HashMap<>();
		List<HljtSchoolnotify> list = new ArrayList<>();
		if(null !=temp) {
			for(HljtSchoolnotify hljtSchoolnotify: temp) {
			int i =0;
		    Integer key = hljtSchoolnotify.getId();
		    HljtSchoolnotify value = hljtSchoolnotify;
		    if(map.containsKey(key)) {
		        HljtSchoolnotify ls = map.get(key);
		        List<String> s =  ls.getImagePathes();
		        try {
		        	 if(value.getType().equals("1") || value.getType().equals("3")) {
		        		 s.add( WhetherNotToUpdate.getIp()+hljtSchoolnotify.getImagePath());
		        	 }else if(value.getType().equals("2") || value.getType().equals("4")) {
						 s.add(value.getImagePath());
					}
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
		    }else {
		    	List<String> s = new ArrayList<>();
		    	 try {
		    		 if(value.getType().equals("1") || value.getType().equals("3")) {
		    			 s.add(WhetherNotToUpdate.getIp()+value.getImagePath());
					}else if(value.getType().equals("2") || value.getType().equals("4")) {
						 s.add(value.getImagePath());
					}
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				 value.setImagePathes(s);
		        map.put(key, value);
		        list.add(value);
		    }
			i=i++;
		}
		}
		return list;
	}

	@Override
	@Transactional(readOnly=true)
	public List<HljtSchoolnotify> campusNotification(HljtSchoolnotify hljtSchoolnotify) {
		return hljtCampusDao.campusNotification(hljtSchoolnotify);
	}

	@Override
	@Transactional
	public boolean addSchoolnotify(HljtSchoolnotify hljtSchoolnotify,List<String> list){
		Integer temp=hljtCampusDao.addSchoolnotify(hljtSchoolnotify);
		if(temp>0) {
			List<HljtNewsaddress> t = new ArrayList<>();
			for (String s : list) {
				HljtNewsaddress f = new HljtNewsaddress();
				f.setImagePath(s);
				f.setSchoolnotifyid(hljtSchoolnotify.getId());
				t.add(f);
			}
			Integer hl  = hljtCampusDao.addNewsaddress(t);
			if(hl==t.size()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean deleteSchoolnotifyById(Integer id) {
		if(hljtCampusDao.deleteSchoolnotifyById(id)>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updateSchoolnotifyById(HljtSchoolnotify hljtSchoolnotify) {
		if(hljtCampusDao.updateSchoolnotifyById(hljtSchoolnotify)>0) {
			if(null!=hljtSchoolnotify.getAddressId()) {
				hljtCampusDao.updateNewsaddressById(hljtSchoolnotify);
			}
			return true;
		}
		return false;
	}

}
